/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.sys.web.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.MessageSource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.mttang.framework.core.paginated.web.controller.AbstractController;
import com.mttang.sys.biz.action.MenuAction;
import com.mttang.sys.dto.MenuDTO;

/**
 * 类MenuActionController.java的实现描述：TODO 类实现描述
 * 
 * @author 郑斌 2010-6-9 上午09:16:18
 */
public class MenuActionController extends AbstractController {

	
	private MenuAction menuAction; 
	
    public void setMenuAction(MenuAction menuAction) {
		this.menuAction = menuAction;
	}

	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 找到本controller在APP中对应的Action.
        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            Map<String, Object> condition = new HashMap<String, Object>();
            Map<Object, Object> model = menuAction.query(condition);
            return new ModelAndView("menu/menuquery", "bean", model);
        } catch (Exception e) {
        	logger.error(e.getStackTrace(),e);
            return new ModelAndView("error", "bean", "Oops, error occur...");
        }
    }

    public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 找到本controller在APP中对应的Action.
        MessageSource msg = (MessageSource) this.getApplicationContext().getBean("messageSource");
        Map<String, Object> condition = new HashMap<String, Object>();
        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            // 获取参数
            String menuName = request.getParameter("menuName");
            String menuUrl = request.getParameter("menuUrl");
            String showlevel = request.getParameter("showlevel");
            String menuPid = request.getParameter("menuPid");
            String menu_pname=request.getParameter("menu_pname");
            String[] prvId = request.getParameterValues("menu_prv");
            
            if(menuPid==null||"".equals(menuPid)){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuPid.error", null, Locale.CHINA));
            }
            if(menuName==null||"".equals(menuName)||menuName.length()>20){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuName.error", null, Locale.CHINA));
            }
            if(menuUrl==null||"".equals(menuUrl)||menuUrl.length()>255){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuUrl.error", null, Locale.CHINA));
            }
            if(showlevel==null||"".equals(showlevel)||showlevel.length()>3){
                return new ModelAndView("error", "bean", msg.getMessage("sys.showlevel.error", null, Locale.CHINA));
            }
            // 组装输入参数
            condition.put("menuName", menuName);
            condition.put("menuUrl", menuUrl);
            condition.put("showlevel", Integer.valueOf(showlevel));
            condition.put("menuPid", Integer.valueOf(menuPid));
            condition.put("prvId", prvId);
            condition.put("menu_pname",menu_pname );
          //日志
            Map<Object, Object> logMap = this.getLogParaInfo(request);
            logMap.put("ITEM",  menuName);//操作数据项：这里是游戏名称
            logMap.put("CLASS", "2");//操作类型
            logMap.put("NEWMAP", condition);
            condition.put("LOGMAP", logMap);
            menuAction.add(condition);
            return new ModelAndView(new RedirectView("menu.do?method=query&prv=query"), "bean", null);
        } catch (Exception e) {
        	logger.error(e.getStackTrace(),e);
            return new ModelAndView("menu/menuadd", "bean", condition);
        }
    }

    public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MessageSource msg = (MessageSource) this.getApplicationContext().getBean("messageSource");
        Map<String, Object> condition = new HashMap<String, Object>();
        String menuId = request.getParameter("menuId");
        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            // 获取参数
            String menuName = request.getParameter("menuName");
            String menuUrl = request.getParameter("menuUrl");
            String showlevel = request.getParameter("showlevel");
            String[] prvId = request.getParameterValues("menu_prv");
            if(menuId==null||"".equals(menuId)){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuId.error", null, Locale.CHINA));
            }
            if(menuName==null||"".equals(menuName)||menuName.length()>20){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuName.error", null, Locale.CHINA));
            }
            if(menuUrl==null||"".equals(menuUrl)||menuUrl.length()>255){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuUrl.error", null, Locale.CHINA));
            }
            if(showlevel==null||"".equals(showlevel)||showlevel.length()>3){
                return new ModelAndView("error", "bean", msg.getMessage("sys.showlevel.error", null, Locale.CHINA));
            }
            
            // 组装输入参数
            condition.put("menuName", menuName);
            condition.put("menuUrl", menuUrl);
            condition.put("showlevel", Integer.valueOf(showlevel));
            condition.put("menuId", Integer.valueOf(menuId));
            condition.put("prvId", prvId);
          //日志
            Map<Object, Object> logMap = this.getLogParaInfo(request);
            logMap.put("ITEM",  menuName);//操作数据项：这里是游戏名称
            logMap.put("CLASS", "1");//操作类型
            logMap.put("NEWMAP", condition);
            condition.put("LOGMAP", logMap);
            menuAction.add(condition);
           
            return new ModelAndView(new RedirectView("menu.do?method=query&prv=query"), "bean", null);
        } catch (Exception e) {
        	logger.error(e.getStackTrace(),e);
            return new ModelAndView("error", "bean", "error");
        }
    }

    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

        MessageSource msg = (MessageSource) this.getApplicationContext().getBean("messageSource");
        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            // 获取参数
            String menuId = request.getParameter("menuId");
            if(menuId==null||"".equals(menuId)){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuId.error", null, Locale.CHINA));
            }
            Map<String, Object> condition = new HashMap<String, Object>();
            // 组装输入参数
            condition.put("menuId", Integer.valueOf(menuId));
          //日志
            Map<Object, Object> logMap = this.getLogParaInfo(request);
            logMap.put("ITEM",  menuId);
            logMap.put("CLASS", "0");
            logMap.put("NEWMAP", condition);
            condition.put("LOGMAP", logMap);
            Map<Object, Object> model = menuAction.delete(condition);
            if(model!=null&&"1".equals(model.get("menuid"))){
                return new ModelAndView("error", "bean",  msg.getMessage("sys.menu.son", null, Locale.CHINA));
            }else{
            return new ModelAndView(new RedirectView("menu.do?method=query&prv=query"), "bean", model);
            }
        } catch (Exception e) {
        	logger.error(e.getStackTrace(),e);
            return new ModelAndView("error", "bean", "Oops, error occur...");
        }
    }

    public ModelAndView searchById(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 找到本controller在APP中对应的Action.
        MessageSource msg = (MessageSource) this.getApplicationContext().getBean("messageSource");
        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            // 获取参数
            String menuId = request.getParameter("menuId");
            if(menuId==null||"".equals(menuId)){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuId.error", null, Locale.CHINA));
            }
            // 组装输入参数
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("menuId", Integer.valueOf(menuId));
          
            Map<Object, Object> model =   menuAction.searchById(condition);
            Map<Object, Object> list = getPrvList(condition);
            model.put("prvlist", list);
            return new ModelAndView("menu/menuupdate", "bean", model);
        } catch (Exception e) {
        	 logger.error(e.getStackTrace());
            return new ModelAndView("error", "bean", "Oops, error occur...");
        }
    }

    public ModelAndView menuAdd(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 找到本controller在APP中对应的Action.
        MessageSource msg = (MessageSource) this.getApplicationContext().getBean("messageSource");
      
        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            // 获取参数
            String menuId = request.getParameter("pid");
            Map<String,Object> condition = new HashMap<String, Object>();
            condition.put("menuId", menuId);
            // 组装输入参数
            Map<Object, Object> model = getPrvList(condition);
            if(!"-1".equals(menuId)){
            	//map  ===>  MenuDTO  
            if("3".equals(((MenuDTO)model.get("menu")).getMenuLevel().toString())){
                return new ModelAndView("error", "bean", msg.getMessage("sys.menuLevel.error", null, Locale.CHINA));  
            }
            }
            model.put("menuPid", menuId);
            return new ModelAndView("menu/menuadd", "bean", model);
        } catch (Exception e) {
        	logger.error(e.getStackTrace(),e);
            return new ModelAndView("error", "bean", "error"); 
        }
    }

    private Map<Object, Object> getPrvList(Map<String,Object> condition) {   
        Map<Object, Object> model = menuAction.menuadd(condition);
        return model;
    }
    
    
    public ModelAndView top(HttpServletRequest request, HttpServletResponse response) throws Exception {

  
        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            Map<String,Object> map = new HashMap<String, Object>();
            Map<Object, Object> model = menuAction.top(map);
            return new ModelAndView("top", "bean", model);
        } catch (Exception e) {
        	logger.error(e.getStackTrace(),e);
            return new ModelAndView("error", "bean", "Oops, error occur...");
        }
    }
    
    public ModelAndView left(HttpServletRequest request, HttpServletResponse response) throws Exception {


        try {
            if (logger.isInfoEnabled()) {
                logger.info("调用远程接口: menuAction");
            }
            String menuId=request.getParameter("menuId");
            if(menuId==null||"".equals(menuId)){
                return new ModelAndView("error", "bean", "Oops, error occur...");
            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("menuId", Integer.parseInt(menuId));
            Map<Object, Object> model = menuAction.left(map);
            return new ModelAndView("left", "bean", model);
        } catch (Exception e) {
        	logger.error(e.getStackTrace(),e);
            return new ModelAndView("error", "bean", "Oops, error occur...");
        }
    }
    /**
     * 日志IP和工号
     * @param request
     * @return
     */
    private Map<Object, Object> getLogParaInfo(HttpServletRequest request){
        //写日志用到的信息
        String ip = request.getHeader("x-forwarded-for");
        if ("unknown".equalsIgnoreCase(ip) || ip == null) {
            ip = request.getRemoteAddr();
        }
        
        Map<Object, Object> userMap = (Map<Object, Object>)request.getSession().getAttribute("user");
        if(userMap == null){
            userMap = new HashMap<Object, Object>();
        }
        Map<Object, Object> returnMap = new HashMap<Object, Object>();
        returnMap.put("OPTIONOR", userMap.get("empno"));
        returnMap.put("IP", ip);
        
        return returnMap;
    }
}
